var _month_name_es = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
var dir_image = "class/images/";

function changeSpinner(Object, changeValue){
	while (Object.tagName!='DIV'){Object = Object.parentNode;}
	var div = Object;
	var max = parseInt(div.getAttribute('maxValue'));
	var min = parseInt(div.getAttribute('minValue'));
	var type = div.getAttribute('type');
	var input = div.getElementsByTagName('input');
	if (input!=null){
		var value = parseInt(input[0].value)+changeValue;
		if (value>max){
			input[0].value = max;
		}
		else if (value<min){
			input[0].value = min;
		}
		else {
			input[0].value = value;
		}
	}
}
function es_bisiesto(year){
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true : false;
}
function calendarPC(date){
	this.minYear=1800;
	this.position="absolute";
	this.year;
	this.month;
	this.day;
	this.date="";
	this.lang="es";
	this.month_name = eval("_month_name_"+this.lang);
	this.extractDate = _extractDate;
	this.show = _showCalendar;
	this.showTable = _showTableCalendar;
	this.divCalendar;
	this.update = _showUpdateTable;	
	this.txtDate = date;
	this.extractDate(date.value);
}
function updateCalendar(Object, day){
	var div=Object;
	while (div.id!="divCalendar") div = div.parentNode;
	var inputs = div.getElementsByTagName("input");
	var txtFecha=null;
	var txtSpinner=null;
	var month="";
	var year="";
	for (var i=0;i<inputs.length;i++){
		if (inputs[i].className=='txtDate') txtFecha = inputs[i];
		if (inputs[i].className=='txtSpinner') txtSpinner = inputs[i];
	}
	if (day==1){
		var newDate = new Date();
		var day=newDate.getDate();
		month=newDate.getMonth() +1;
		year=newDate.getFullYear();
		txtFecha.value = day+"/"+month+"/"+year;
		//var cp = new calendarPC();
	}
	else {
		var selects = div.getElementsByTagName("select");
		var cmonth = selects[0];
		var cp = new calendarPC(txtFecha.value);
		month = cmonth.value;
		year = txtSpinner.value;
	}
	var divShow=Object;
	while (divShow.id!="divShowCalendar") divShow = divShow.parentNode;
	//alert (divShow.innerHTML);
	cp.update(divShow, month, year);
}
function _showCalendar(div, pos){
	this.divCalendar = div;
	if (this.date=="") return;
	d=0;l=1;m=2;x=3;j=4;v=5;s=6;
	if (this.position=="absolute"){
		if (pos==null){ pos = new array();pos[0]=0;pos[1]=0;}
		//div.parentNode.style.position="relative";
		div.style.position="relative";
		//div.style.top=pos[1];
		//div.style.top="0px";
		//div.style.left=pos[0];
		//div.style.top=pos[1];
		//div.style.top=pos[1]+"px";
		//div.style.top="100px";
		//div.style.left=pos[0]+"px";
		//div.style.left="200px";
		//alert (div.style.top);
	}	
	rpta="<div style='position:absolute;left:20px"+";top:0px;'>";
	rpta+=this.showTable(this.day, this.month, this.year);
	rpta+="</div>";
	div.innerHTML=rpta;
}
function _showUpdateTable(div, month, year){
	rpta="";
	rpta="<div style='position:absolute;left:20px"+";top:0px;'>";
	rpta+=this.showTable(this.day, month, year);
	rpta+="</div>";
	div.innerHTML=rpta;
}

function _showTableCalendar(day, month, year){
	var cnt = _cntBisiestos(year-1);
	var dias = 3+(year-this.minYear)+cnt;
	dias = dias % 7;
	dias = _getDayMonth(dias, month, year);
	rpta="";
	rpta+="<div class='divBackground'>";
	rpta+="<table border=0 cellspacing=2 cellpadding=2>";
	rpta+="<tr class='trTitle'> <th colspan=7> Calendario <img src='"+dir_image+"time.gif' style='float:left' title='Hoy' onclick='updateCalendar(this,1)'></img> <img src='"+dir_image+"cancel.gif' style='float:right' title='Salir' onclick='_calendar_exit(this);'></img></th></tr>"
	rpta+="<tr class='trTitle'> <th colspan=7> <select onchange='updateCalendar(this);'>";
	for (var i=0;i<12;i++){
		rpta+="<option value='"+(i+1)+"' ";
		if (month==i+1) rpta+=" selected ";
		rpta+=">"+this.month_name[i]+"</option>";
	}
	rpta+="</select>";
	
	rpta+="<div id='divSpinner' maxValue='2200' minValue='1900' style='float:right'>";
	rpta+="<table border=0 cellspacing=0 cellpadding=0 class='tableCalendar'>";
	rpta+="<tr><td rowspan=2><input type='text' size='3' maxlength='4' class='txtSpinner' value='"+year+"' onblur='updateCalendar(this);'></td>";
	rpta+="	<td><img class='imgCalendar' src='"+dir_image+"move_up.gif' onclick='changeSpinner(this,1);updateCalendar(this);'></img></td>";
	rpta+="</tr>";
	rpta+="<tr><td><img class='imgCalendar' src='"+dir_image+"move_down.gif' onclick='changeSpinner(this,-1);updateCalendar(this);'></img></td>";
	rpta+="</tr>";
	rpta+="</table>";
	rpta+="</div>";
	
	rpta+="</th></tr>";
	
	
	rpta+="<tr class='trTitle'><th>D</th><th>L</th><th>M</th><th>X</th><th>J</th><th>V</th><th>S</th></tr>";
	rpta+="<tr>";
	var rows=1;
	for (var i=0;i<dias;i++,rows++){
		rpta+="<td>&nbsp</td>";
	}
	cntDias=31;
	if (month==2){
		cntDias=28;
		if (es_bisiesto(year)) cntDias=29;
	}
	else if (month<=8){
		cntDias = 30 + (month%2==0?0:1);
	}
	else {
		cntDias = 30 + (month%2==0?1:0);
	}
	for (var i=1;i<=cntDias;i++,rows++){
		rpta+="<td onmouseover='_calendarMouseDay(this,1)' onmouseout='_calendarMouseDay(this,0)' onclick='_calendarSelectDay(this,"+i+","+day+","+month+","+year+");'";
		if (i==this.day && month==this.month && year==this.year) {rpta+=" class='trCurrentDay' ";}
		rpta+=" id='tr"+i+"'>"+i+"</td>";
		if (rows%7==0)rpta+="</tr><tr>";
	}
	rpta+="</tr>";
	rpta+="</table>";
	rpta+="</div>";
	return rpta;
}
function _calendar_exit(Object){
	while (Object.id!="divShowCalendar") Object = Object.parentNode;
	Object.innerHTML="";
}
function _calendarSelectDay(Object, dayCurrent, day, month, year){
	var div = Object;
	//while (div.tagName!='DIV') div = div.parentNode;
	while (div.className!='pCalendar') div = div.parentNode;
	var input = div.getElementsByTagName('input')[0];
	var fecha = input.value;
	fecha = fecha.split("/");
	var tds = div.getElementsByTagName('td');
	for (var i=0;i<tds.length;i++){
		_changeClass(tds[i],'trUnSelectDay');
	}
	
	dayCurrent=dayCurrent+"";
	month=month+"";
	year=year+"";
	if (dayCurrent.length!=2) dayCurrent = "0"+dayCurrent;
	if (month.length!=2) month = "0"+month;
	if (year.length==2) year = "19"+year;
	var fecha = dayCurrent+"/"+month+"/"+year
	input.value = fecha;
	_changeClass(Object,'trCurrentDay');
}
function _calendarMouseDay(Object, type){
	if (type==1){
		if (Object.className!="trCurrentDay")
		_changeClass(Object,'trSelectDay');}
	else		{
		
		if (Object.className!="trCurrentDay"){
			//alert (Object);
			_changeClass(Object,'trUnSelectDay');
		}
	}
}
function _changeClass(Object, className){
	Object.className=className;
}
function _extractDate(date){
	if (date==null || date=="") {
		var newDate = new Date();
		this.day=newDate.getDate();
		this.month=newDate.getMonth() +1 ;
		this.year=newDate.getFullYear();
		this.date= this.day + "/" + this.month + "/" + this.year;
		this.txtDate.value = this.date;
	}else{
		var newDate = date.split("/");
		if (newDate.length!=3) return this.extractDate("");
		this.day=parseInt(newDate[0]*1);
		this.month=parseInt(newDate[1]*1);
		this.year=parseInt(newDate[2]*1);
		if ((this.day)>31 || (this.day)<1) this.day=1;
		if ((this.month)>12 || (this.month)<1) this.month=1;
		if ((this.year)<1800) this.year=this.minYear;
		this.date= this.day + "/" + this.month + "/" + this.year;
	}
}
function _getDayMonth(day, month, year){
	if (month==1) return day;
	else if (month==2) day = day+31;
	else if (month <= 8) {
		day = day + ((month-1)*31)-parseInt((month-1)/2)-2;
		if (es_bisiesto(year)) day++;
	}
	else{
		day = day + (7*31)-parseInt(7/2)-2;
		if (es_bisiesto(year)) day++;
		month=month-8;
		day = day + ((month)*31)-parseInt((month)/2);
	}
	return day%7;
}
function _cntBisiestos(fin){
	var ini=1800;
	var anios = (fin-ini);
	bisiestos = anios/4;
	ini = ini-200;
	rpta = parseInt(bisiestos - parseInt(anios/100) + parseInt((fin-ini)/400));
	return rpta;
}

function _checkDate(txt, e){
	tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8) return true;
	if (tecla==0) return true;
	//if (tecla==47) return true; // simbolo /
	patron = /\d/;
    te = String.fromCharCode(tecla);
    if (!patron.test(te)) return false;
	var value = txt.value;
	if (value.length==2){
		txt.value=value+"/";
	}
	if (value.length==5){
		txt.value=value+"/";
	}
	return true;
}
function _getPosCurrent(e){
	if (document.layers) {
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	} else if (document.all) {        
		xMousePos = window.event.x+document.body.scrollLeft;
		yMousePos = window.event.y+document.body.scrollTop;
		xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
		yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	} else if (document.getElementById) {
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}
	return [xMousePos, yMousePos];
}
function _getDimensionScreen(e){
	if (document.layers) {
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	} else if (document.all) {        
		xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
		yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	} else if (document.getElementById) {
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}
	return [xMousePosMax, yMousePosMax];
}
function showCalendar(e, Object){
	var pos = _getPosCurrent(e);
	while (Object.tagName!="DIV") Object=Object.parentNode;
	var txtFecha = Object.getElementsByTagName('input')[0];
	var divCalendar = Object.getElementsByTagName('div')[0];
	var cp = new calendarPC(txtFecha);
	cp.show(divCalendar,pos);
	
	//alert ("xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax);
}